package com.neepu.controller;


import com.neepu.pojo.Target;
import com.neepu.pojo.TwoTarget;
import com.neepu.service.IndexManagerService;
import com.neepu.util.annotation.ArchivesLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.List;

@Controller
public class IndexManagerController {

    @Autowired
    private IndexManagerService indexManagerService;

    // 一级指标
    @ArchivesLog(operteContent = "查看一级指标")
    @RequestMapping(value = "/target/one")
    public String one(Model model,String content){

        if (content!=null&&!content.equals("")){
            List<Target> list = indexManagerService.findAllTargetByContent(content);
            model.addAttribute("list",list);
            return "indexmanagement/one";
        }else {
            List<Target> list = indexManagerService.getAllTarget();
            model.addAttribute("list",list);
            return "indexmanagement/one";
        }
    }
//  添加一级指标跳转
    @RequestMapping(value = "target/one/add")
    public String oneadd(){
            return "indexmanagement/oneadd";
    }

    // 添加一级指标   /target/toadd
    @RequestMapping(value = "target/one/toadd")
    public String tooneadd(String name,Double weight,Float score,Integer id){

        if (id!=null){
            Target target = new Target();
            target.setId(id);
            target.setName(name);
            target.setWeight(weight);
            target.setScore(score);
            indexManagerService.updateTarget(target);
            return "redirect:/target/one";
        }else{
            Target target = new Target();
            target.setName(name);
            target.setWeight(weight);
            target.setScore(score);
            indexManagerService.insertTarget(target);
            return "redirect:/target/one";
        }
    }


    // 添加二级指标   //target/two/toadd
    @RequestMapping(value = "/target/two/toadd")
    public String totwoadd(Integer oneId,String name,Double weight,Float score,Integer id){
        if(id!=null){
            TwoTarget twoTarget = new TwoTarget();
            twoTarget.setId(id);
            twoTarget.setOneId(oneId);
            twoTarget.setName(name);
            twoTarget.setWeight(weight);
            twoTarget.setScore(score);
            indexManagerService.updateTwoTarget(twoTarget);
            return "redirect:/target/two";
        }else {
            TwoTarget twoTarget = new TwoTarget();
            twoTarget.setOneId(oneId);
            twoTarget.setName(name);
            twoTarget.setWeight(weight);
            twoTarget.setScore(score);
            indexManagerService.insertTwoTarget(twoTarget);
            return "redirect:/target/two";
        }
    }

//  添加二级指标跳转
    @RequestMapping(value = "target/two/add")
    public String twoadd(Model model){
        List<Target> target_list = indexManagerService.getAllTarget();
        model.addAttribute("target_list",target_list);
        return "indexmanagement/twoadd";
    }

    // 二级指标
    @ArchivesLog(operteContent = "查看二级指标")
    @RequestMapping(value = "/target/two")
    public String two(Model model,String content){
        if (content!=null&&!content.equals("")){
            List<TwoTarget> twolist = indexManagerService.findAllTwoTargetByContent(content);
            List<TwoTarget> list  = new ArrayList<>();
            for (TwoTarget twoTarget : twolist) {
                Target target = indexManagerService.findTargetById(twoTarget.getOneId());
                twoTarget.setTarget(target);
                list.add(twoTarget);
            }
            model.addAttribute("list",list);
            return "indexmanagement/two";
        }else {
            List<TwoTarget> twolist = indexManagerService.getAllTwoTarget();
            List<TwoTarget> list  = new ArrayList<>();
            for (TwoTarget twoTarget : twolist) {
                Target target = indexManagerService.findTargetById(twoTarget.getOneId());
                twoTarget.setTarget(target);
                list.add(twoTarget);
            }
            model.addAttribute("list",list);
            return "indexmanagement/two";
        }
    }


    // 删除一级指标 delonetarget
    @RequestMapping(value="/delonetarget",method= RequestMethod.GET)
    public void deleteOne(Integer id){
        if(id!=null){
            indexManagerService.deleteOneTarget(id);
        }
    }

    // 删除二级指标 deltwotarget
    @RequestMapping(value="/deltwotarget",method=RequestMethod.GET)
    public void deleteTwo(Integer id){
        if(id!=null){
            indexManagerService.deleteTwoTarget(id);
        }
    }


    // 一级指标编辑页面跳转
    @RequestMapping(value="/target/edit",method=RequestMethod.GET)
    public String oneedit(Integer id,Model model){
        Target list = indexManagerService.findTargetById(id);
        model.addAttribute("list",list);
        return "indexmanagement/oneadd";

    }

    // 二级级指标编辑页面跳转
    @RequestMapping(value="/target/twoedit",method=RequestMethod.GET)
    public String twoedit(Integer id,Model model){
        TwoTarget list = indexManagerService.findTwoTargetById(id);
        Target target = indexManagerService.findTargetById(list.getOneId());
        list.setTarget(target);
        List<Target> target_list = indexManagerService.getAllTarget();
        model.addAttribute("target_list",target_list);
        model.addAttribute("list",list);
        return "indexmanagement/twoadd";
    }
}
